package com.qualityevaluationcloudoauth2.service;


import com.qualityevaluationcloudoauth2.Repository.MenuInfoRepository;
import com.qualityevaluationcloudoauth2.Repository.RoleFunctionRepository;
import com.qualityevaluationcloudoauth2.Repository.UserInfoRepository;
import com.qualityevaluationcloudoauth2.Repository.UserRoleReRepository;
import com.qualityevaluationcloudoauth2.daomin.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OauthUserService implements UserDetailsService {
    @Autowired
    private UserInfoRepository userInfoRepository;
    /*@Autowired
    private MenuInfoRepository menuInfoRepository;
    @Autowired
    private UserRoleReRepository userRoleReRepository;
    @Autowired
    private RoleFunctionRepository roleFunctionRepository;*/

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        UserInfo userInfo = userInfoRepository.findUserInfoByUsername(userName);
        if (userInfo == null||"0".equals(userInfo.getUsedstatus()))
            return null;
        List<Authority> authorities = new ArrayList<Authority>();
        /*UserRoleRe userRoleRe = userRoleReRepository.findUserRoleReByUserid(userInfo.getId());
        List<RoleFunction> roleFunctionList = roleFunctionRepository.findRoleFunctionsByRoleid(userRoleRe.getRoleid());
        for (RoleFunction roleFunction : roleFunctionList) {
            MenuInfo menuInfo = menuInfoRepository.findById(roleFunction.getMenuid()).get();
            authorities.add(new Authority(menuInfo.getId(), menuInfo.getMenuname()));
        }*/
        //注本数据库user未设置是否过期或锁定
        //增加判断用户是否过期 锁定等状态 如果状态锁定或过期，返回null
        return new User(userInfo.getUsername(), userInfo.getPassword(), true, true, true, true, authorities);
    }



}
